package com.ty.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ty.entity.User;
import com.ty.service.UserService;
import com.ty.service.impl.UserServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/user/query")
public class UserQueryServlet extends HttpServlet {
    private final UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            List<User> users = userService.getAllUsers();
            outputUserTable(users, out);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.println("查询失败：" + e.getMessage());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String username = request.getParameter("username");
            List<User> users;
            if (username == null || username.trim().isEmpty()) {
                users = userService.getAllUsers();
            } else {
                users = userService.getUsersByUsername(username.trim());
            }
            outputUserTable(users, out);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.println("查询失败：" + e.getMessage());
        }
    }

    private void outputUserTable(List<User> users, PrintWriter out) {
        StringBuilder html = new StringBuilder();
        for (User user : users) {
            html.append("<tr>");
            html.append("<td>").append(user.getId()).append("</td>");
            html.append("<td>").append(user.getUsername()).append("</td>");
            html.append("<td>").append(user.getAccount()).append("</td>");
            html.append("<td>").append(user.getPermission() == 1 ? "管理员" : "普通用户").append("</td>");
            html.append("<td><a href=\"#\" class=\"edit-link\" data-id=\"").append(user.getId()).append("\">编辑</a> ");
            html.append("<a href=\"#\" class=\"delete-link\" data-id=\"").append(user.getId()).append("\">删除</a></td>");
            html.append("</tr>");
        }
        html.append("<tr>");
        html.append("<td colspan=\"5\" align=\"center\">共").append(users.size()).append("人</td>");
        html.append("</tr>");
        out.println(html.toString());
    }
} 